package com.xwaf.platform.web.controller.auth;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.constants.BaseConstants;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.entity.DataBase;
import com.xwaf.platform.system.api.entity.auth.ImportLog;
import com.xwaf.platform.system.api.service.auth.ImportLogService;
import com.xwaf.platform.system.api.service.pay.OrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * <p>
 * 导入日志控制器
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2020-1-4 14:13:56 <br>
 * @see 无<br>
 *      Copyright (c) 2020, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@RestController
@Api(tags = "导入日志")
@RequestMapping(value = "/m")
public class ImportLogController extends BaseController {

	@Autowired
	private ImportLogService importLogService;

	@Autowired
	private OrderService orderService;

	@ApiOperation(value = "分页查询", response = ImportLog.class)
	@PostMapping(value = "/importLog/page", produces = "application/json;charset=UTF-8")
	public CommonResponse page(@RequestParam("currentPage") String currentPage,
			@RequestParam("pageSize") String pageSize, @RequestBody ImportLog importLog) {
		CommonResponse resp = CommonResponse.createCommonResponse();
		if (!StringUtils.isNotBlank(currentPage) & !StringUtils.isNotBlank(pageSize)) {
			currentPage = "1";
			pageSize = "10";
		}
		Page<ImportLog> page = new Page<ImportLog>(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
		Page<ImportLog> result = importLogService.selPageList(page, importLog);
		resp.setData(result);
		return resp;
	}

	@ApiOperation(value = "根据条件查询导入日志", notes = "根据条件查询导入日志信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "/importLogs", produces = "application/json;charset=UTF-8")
	public CommonResponse list(@RequestParam("name") String name) {
		Wrapper<ImportLog> wrapper = new EntityWrapper<ImportLog>();
		wrapper.like("name", name);
		List<ImportLog> list = importLogService.selectList(wrapper);
		return CommonResponse.createCommonResponse(list);
	}

	@ApiOperation(value = "新增导入日志", notes = "新增导入日志信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping(value = "/importLog", produces = "application/json;charset=UTF-8")
	public CommonResponse addImportLog(@RequestBody ImportLog importLog) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			importLog.setCreateBy(loginUser.getId());
			importLog.setUpdateBy(loginUser.getId());
		}
		importLog.setId(DataBase.getDataBaseId(DataBase.IL));
		boolean isAdded = importLogService.insert(importLog);
		if (isAdded) {
			return CommonResponse.createCommonResponse(importLog);
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "新增失败!");
		}
	}

	@ApiOperation(value = "修改导入日志", notes = "修改导入日志信息", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	@PutMapping(value = "/importLog/{importLogId}", produces = "application/json;charset=UTF-8")
	public CommonResponse updateImportLog(@PathVariable("importLogId") String importLogId,
			@RequestBody ImportLog importLog) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			importLog.setUpdateBy(loginUser.getId());
		}
		importLog.setId(importLogId);
		boolean isUpdated = importLogService.updateById(importLog);
		if (isUpdated) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "修改失败!");
		}
	}

	@ApiOperation(value = "删除导入日志", notes = "删除导入日志信息", hidden = true, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@DeleteMapping(value = "/importLog/{importLogIds}", produces = "application/json;charset=UTF-8")
	public CommonResponse delById(@PathVariable("importLogIds") List<String> importLogIds) {
		// boolean isDel = importLogService.deleteBatchIds(importLogIds);
		boolean isDel = true;
		if (isDel) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "删除失败!");
		}
	}

	@ApiOperation(value = "查看导入日志详情", notes = "查看导入日志详情信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "/importLog/{importLogId}", produces = "application/json;charset=UTF-8")
	public CommonResponse getInfoById(@PathVariable("importLogId") String importLogId) {
		ImportLog importLog = importLogService.selectById(importLogId);
		return CommonResponse.createCommonResponse(importLog);
	}

	@ApiOperation(value = "导入", notes = "导入", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/importData", produces = "application/json;charset=UTF-8")
	public CommonResponse importData(HttpServletRequest reques, @RequestBody ImportLog importLog) {
		ELUser loginUser = getLoginUser();
		Locale local = LocaleContextHolder.getLocale();
		// 绝对路径
		String filePath = request.getServletContext().getRealPath(importLog.getFilePath());
		// 错误相对路径
		String saveErrorUrl = request.getServletContext()
				.getRealPath(importLog.getFilePath().substring(0, importLog.getFilePath().lastIndexOf("/")));
		// 错误的文件名
		String saveErrorFile = importLog.getFilePath().substring(importLog.getFilePath().lastIndexOf("/") + 1);
		int num = importLogService.importOrder(importLogService, orderService, importLog, loginUser, filePath,
				importLog.getFileName(), saveErrorUrl, saveErrorFile, local);
		return CommonResponse.createParamCommonResponse(num > 0 ? true : false, num > 0 ? "200" : "500",
				importLog.getId());
	}

}
